import util from "../../common/js/util.js";
var app = getApp();
var base_width = 60;
var base_height = 39
/**
 * 采购比价 专用表格
 */


Component({
  mixins: [],
  data: {
    labelList1: [
      { text: "序号", name: "index" },
      { text: "预计使用时间", name: "TIME5_VALUE" },//TIME5
      { text: "类别", name: "CATEGORY_NAME" },
      { text: "品牌要求", name: "BRAND" },

      { text: "品名", name: "MATERIAL_NAME" },
      { text: "规格", name: "MODEL" },
      { text: "单位", name: "METERING_NAME" },
      { text: "数量", name: "NUM" },
      // { text: "固定资产", name: "FIXED_ASSETS_VALUE" },
      // 项目部预估单价
      { text: "项目部预估单价", name: "ESTIMATED_PRICE" },
    ],
    dataList1: [
      // {
      //   TIME5: "2022-03-29",
      //   MATERIAL_NAME: "镀锌铁丝",
      //   FIXED_ASSETS: "否",
      //   MODEL: "22#",
      //   NOTE: "",
      //   NUM: "6",
      //   METERING_NAME: "/",
      //   BRAND: "无"
      // },
      // {
      //   TIME5: "2022-03-29",
      //   MATERIAL_NAME: "铁棒",
      //   FIXED_ASSETS: "否",
      //   MODEL: "22#",
      //   NOTE: "",
      //   NUM: "6",
      //   METERING_NAME: "/",
      //   BRAND: "无"
      // }
    ],
    labelTitleList2: [
      { text: "需另付运费(元)", name: "Freight" },
      { text: "合计", name: "UnitPrice" },
      { text: "推荐供应商各自合计", name: "TotalPrice" },
      { text: "裸票总价", name: "NakedTicket" },
      { text: "含票总价", name: "IncludingTickets" },
      { text: "票据类型", name: "TicketType" },
      { text: "发票税率(%)", name: "TicketTax" },
      // { text: "是否含运费", name: "Freight" },
      { text: "供应完成时间", name: "DeliveryTime" },
      { text: "付款方式", name: "PaymentMethod" },
      { text: "询价人", name: "Inquiry" },
      { text: "询价证明", name: "file" },
      { text: "备注", name: "NOTE" },
      { text: "推荐供应商理由", name: "recommend" },
    ],
    // labelList2: [
    //   { text: "供应商1号"}
    // ],
    dataList2: [
      { SUPPLIER_NAME: "供应商1号" }
    ],
    data2TopList: [],
    tableHeight: 0,
    tableWidth1: 0,
    tableWidth2: 0,
    scrollY2_1: 0,
    scrollY2_2: 0,
    left: 0,
    right: 0,
    file_list: [],
    SUPPLIER_NAME: "",
    showPopup: false,
    disMove1: false,
    move1: true,
    disMove2: false,
    move2: true,
    tab2_width:23,//max-width:50%;min-width:23%
  },
  props: {},
  onInit() {
    //console.log("--init");
    let json = { SUPPLIER_NAME: "供应商1号", "NOTE": "", "Inquiry": "刘庆峰", "Freight": "1", "TotalPrice": 11, "UnitPrice": 11, "TicketTax": "1", "TicketType": "增值税电子普通发票", "NakedTicket": 0, "CHOOSE_TOTAL": 0, "PaymentMethod": "1", "IncludingTickets": 0, "index": 0, "add_type": "", "DeliveryTime": "2022-03-29", "name": "supplierName0", "Inquiry_arr": ["liuqingfeng"], "PRICECOMPARISON_ID": "2b27563e3fe64b1eab06de6191d695b4", "stepName": "", recommend: "暂无" };
    json.PRICE_LIST = [];
    json.PRICE_LIST.push({ unitPrice: 1, totalPrices: 10 });
    json.PRICE_LIST.push({ unitPrice: 2, totalPrices: 20 });
    let dataList2 = [json];

    this.setData({
     // tableHeight: (this.data.dataList1.length+ this.data.labelTitleList2.length) * base_height - 6,
      tableHeight: (this.data.dataList1.length+1 + this.data.labelTitleList2.length) * base_height,
      tableWidth1: base_width * (this.data.labelList1.length) + 6,
      tableWidth2: base_width * (dataList2.length) + 6,
      dataList2,
    })
    //let dataList2=[]

    //  console.log(`tableHeight：${this.data.tableHeight}`);
    // console.log(`tableWidth: ${this.data.tableWidth}`);
  },
  didMount() { },
  didUpdate() { },
  didUnmount() { },
  methods: {
    tableItemClick(e) {
      let value = e.currentTarget.dataset.value;
      
      dd.alert({
        content: value
      })
    },
    tableItemClick2(e){
      let pindex = e.currentTarget.dataset.pindex;
      let cindex = e.currentTarget.dataset.cindex;

      let node=this.data.data2TopList[pindex].list[cindex];
      dd.alert({
        content: `单价:${node.unitPrice} 总价:${node.totalPrices}` 
      })
    },
    tableItemClick3(e){
      let value = e.currentTarget.dataset.value;
      dd.alert({
        content: value
      })

    },
    onTouchStart1() {
      this.setData({
        disMove1: true,
        move1: false
      })      
    },
    onTouchEnd1() {
      this.setData({
        disMove1: false,
        move1: true
      })
    },
    onTouchStart2() {
      this.setData({
        disMove2: true,
        move2: false
      })      
    },
    onTouchEnd2() {
      this.setData({
        disMove2: false,
        move2: true
      })
    },
    bottomItemClick(e) {
      //console.log(this.data.dataList2);
      let index = e.currentTarget.dataset.index;
      let file_list = []
      const element = this.data.dataList2[index];

      let SUPPLIER_NAME = element.SUPPLIER_NAME;
      let PID = element.PRICECOMPARISON_ID;
      let data = {
        TYPE_NAME: "PurchasePriceComparison",
        PID
      }
      app.$post("/YhERP/enclosure/json/enclosureList", data).then(res => {
        if (res.is == 1) {
          res.data.forEach(e => {
            e.url = app.prototype.$urls + e.PATH;
            e.name = e.FILE_NAME;
          })
          file_list = res.data;
          this.setData({
            file_list, SUPPLIER_NAME
          })
          this.image_item_group.initData(file_list);
          this.onOpenPopup();
        }
      });


    },
    openFile(e) {

    },
    image_item_group(ref) {
      this.image_item_group = ref;
    },
    operatorDataTop2(dataList2) {
      if(util.isEmpty(dataList2)){
        return;
      }
      let data2TopList = [];
      let length = dataList2[0].PRICE_LIST.length;
      for (let index = 0; index < length; index++) {
        let json = { list: [] };
        for (let b = 0; b < dataList2.length; b++) {
          const element = dataList2[b].PRICE_LIST[index];
          json.list.push(element);
        }
        data2TopList.push(json);
      }
      this.setData({
        data2TopList
      })
     // console.log("----data2TopList:",data2TopList);

    },
    initData(dataList1, dataList2,cate_is_empty) {
      let tableHeight = (dataList1.length + this.data.labelTitleList2.length) * base_height - 6;
      // console.log(dataList1);
      let tab2_width=this.data.tab2_width;
      if(dataList2.length>1){
        tab2_width=50;

      }
      if(cate_is_empty==0){
        let labelList1 = this.data.labelList1;
        let labelList = labelList1.filter(e => {
          return e.name!='CATEGORY_NAME';
        })
        this.setData(
          {
            labelList1:labelList
          });
      }
      this.setData(
        {
          dataList1,
          dataList2,
          tableHeight,
          tableWidth2: base_width * (dataList2.length) + 60,
          tab2_width
        }
      )
      
      this.operatorDataTop2(dataList2);

    },
    scrollY(e) {
      this.setData({
        scrollTop: e.detail.scrollTop,
      });
    },
    scrollY2_1fn(e) {
      this.setData({
        //   scrollY2_1: e.detail.scrollTop,
        scrollY2_2: e.detail.scrollTop,

      });
    },
    scrollY2_2fn(e) {
      this.setData({
        //  scrollY2_2: e.detail.scrollTop,
        scrollY2_1: e.detail.scrollTop,
      });
    },
    scrollX2_1(e) {

      // console.log(e.detail.scrollLeft);
      this.setData({
        right: 0,
        left: 1,
        scrollX2_1: e.detail.scrollLeft,
      });

    },
    scrollX2_2(e) {
      // console.log(e.detail.scrollLeft);

      this.setData({
        right: 1,
        left: 0,
        scrollX2_2: e.detail.scrollLeft,
      });
    },
    scrollX(e) {
      // console.log(e.detail.scrollLeft);
      this.setData({
        scrollLeft: e.detail.scrollLeft,
      });
    },
    onClosePopup() {
      this.setData({
        showPopup: false
      })
    },
    onOpenPopup() {
      this.setData({
        showPopup: true
      })
    },
    i_popover(ref){
      this.i_popover=ref;
    },
    openPopover(e){
      let index=e.currentTarget.dataset.index;
      let pindex=e.currentTarget.dataset.pindex;
      let obj=this.data.dataList1[pindex];
     // console.log(obj);
      
      this.i_popover.openFn(obj);
    }
  },
});
